<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Order_Model extends CI_Model
{
	function get_store($user_id){
		$this->db->where('user_id', $user_id);

		$query = $this->db->get('store');
		if ($query->num_rows() == 1) return $query->row();
		return NULL;

	}
	
	function get_order_by_store($store_id='', $page='', $limit=''){
		$query = $this->db->query('SELECT * FROM (`order`) WHERE order_id in (select order_id from order_product where store_id = "'.$store_id.'")');
		
		if(!empty($limit))
		$query = $this->db->query('SELECT * FROM (`order`) WHERE order_id in (select order_id from order_product where store_id = "'.$store_id.'") limit '.$page.','.$limit.'');
		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();	
	}

	function get_order_status(){
		$query = $this->db->get('order_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_by_id($order_id){
        return $this->db->get_where('order', array('order_id'=>$order_id))->row();
	}
	
	function get_order_product_by_order($order_id){
		$this->db->where('order_id', $order_id);

		$query = $this->db->get('order_product');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_total($order_id){
		$this->db->where('order_id', $order_id);

		$query = $this->db->get('order_total');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function sum_total($order_id){
		$this->db->where('order_id', $order_id);
		$this->db->select_sum('value');
		$query = $this->db->get('order_total');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_product_by_store($store_id){
		$this->db->where('store_id', $store_id);
		$query = $this->db->get('order_product');
			
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}
	
	function get_order_product($order_id, $store_id){
		$this->db->where('order_id', $order_id);
		$this->db->where('store_id', $store_id);
		$query = $this->db->get('order_product');
		
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function sum_order_product($order_id, $store_id){
		$this->db->where('order_id', $order_id);
		$this->db->where('store_id', $store_id);
		$this->db->select_sum('subtotal');
		$query = $this->db->get('order_product');
		
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function update_status($order_id){
		$data = array(
					'order_status_id' => $this->input->post('order_status_id'),
					'payment_status_id' => $this->input->post('payment_status_id'),
					'date_modified' => date ('Y-m-d H:i:s')
				);
		$this->db->where('order_id', $order_id);
        $this->db->update('order', $data);
	}
	
	function add_history($order_id){
		$data = array(
					'order_id'		  => $order_id,
					'order_status_id' => $this->input->post('order_status_id'),
					'payment_status_id' => $this->input->post('payment_status_id'),
					'comment' 		  => $this->input->post('comment'),
					'date_added' 	  => date ('Y-m-d H:i:s')
		);
		$this->db->where('order_id', $order_id);
        $this->db->insert('order_history', $data);
	}
	
	function get_history($order_id, $page='', $limit=''){
		
		$this->db->where('order_id', $order_id);
		if(!empty($limit))
		$this->db->limit($limit,$page);
		
		$query = $this->db->get('order_history');
		
		//echo $this->db->last_query();
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}
	}
	
	function update($order_id, $data){
		$this->db->where('order_id', $order_id);
        $this->db->update('order', $data);
	}
	
	function get_payment_status(){
		$query = $this->db->get('payment_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_customer_by_user($user_id){
		$this->db->where('user_id', $user_id);
		$query = $this->db->get('customer');
			
		if ($query->num_rows() > 0) return $query->row();
		return NULL;
	}
	
	function get_order_by_customer($customer_id = '', $page='', $limit=''){
		$this->db->where('customer_id', $customer_id);
		$this->db->order_by('date_added','desc');
		$this->db->group_by('order_cid');
		
		if(!empty($limit))
		$this->db->limit($limit,$page);
		
		$query = $this->db->get('order');
		//echo $this->db->last_query();	
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
		
	}
	
	function sum_total_by_cid($customer_id, $order_cid){
		$this->db->select_sum('total');
		$this->db->where('customer_id', $customer_id);
		$this->db->where('order_cid', $order_cid);
		$this->db->group_by('order_cid');
		$query = $this->db->get('order');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function count_product_by_cid($customer_id, $order_cid){
		$query = $this->db->query('select count(order_product_id) as count_product from order_product where order_id in(select order_id from `order` where customer_id = "'.$customer_id.'" 									and order_cid = "'.$order_cid.'")');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_by_cid($order_cid){
        return $this->db->get_where('order', array('order_cid'=>$order_cid))->row();
	}
	
	function get_order_products_by_cid($order_cid){
		$query = $this->db->query('select * from order_product where order_id in(select order_id from `order` where order_cid = "'.$order_cid.'")');
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function order_total_by_cid($order_cid){
		$query = $this->db->query('select * from order_total where order_id in(select order_id from `order` where order_cid = "'.$order_cid.'")');
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function sub_total_order_by_cid($order_cid){
		$query = $this->db->query('select sum(subtotal) as subtotal from order_product where order_id in (select order_id from `order` where order_cid = "'.$order_cid.'")');
		
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function order_total_by_cid_code($order_cid, $code){
		return $query = $this->db->query('select sum(value) as value from order_total where order_id in (select order_id from `order` where order_cid = "'.$order_cid.'") and code = "'.$code.'" ')->row();
		
		//echo $this->db->last_query();
		
	}
	
	function get_shipping_by_order($order_id){
		$this->db->where('order_id',$order_id);
		$query = $this->db->get('order_shipping');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_product_by_id($product_id){
		return $query = $this->db->query('select * from product where product_id = "'.$product_id.'"')->row();
	}
	
	function get_status_payment(){
		$query = $this->db->get('payment_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_option_by_order($order_id){
		$this->db->where('order_id',$order_id);
		$query = $this->db->get('order_option');
		if ($query->num_rows() > 0) return $query->row();
		return NULL;
	}
	
	function get_order_option_by_order_product($order_product_id){
		$this->db->where('order_product_id', $order_product_id);
		$query = $this->db->get('order_option');
		if ($query->num_rows() > 0) return $query->row();
		return NULL;
	}

}
